/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.snake;

import java.util.HashSet;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdView;

/**
 * Snake: a simple game that everyone can enjoy.
 * 
 * This is an implementation of the classic Game "Snake", in which you control a
 * serpent roaming around the garden looking for apples. Be careful, though,
 * because when you catch one, not only will you become longer, but you'll move
 * faster. Running into yourself or the walls will end the game.
 * 
 */
public class Snake extends Activity implements AdListener {

  private SnakeView mSnakeView;
  private AdView mAdView;

  private static String ICICLE_KEY = "snake-view";

  /**
   * Called when Activity is first created. Turns off the title bar, sets up the
   * content views, and fires up the SnakeView.
   * 
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.snake_layout);

    mSnakeView = (SnakeView) findViewById(R.id.snake);
    mSnakeView.setTextView((TextView) findViewById(R.id.text));

    // START AD REQUEST CODE
    
    // See snake_layout.xml for the specification for R.id.adView
    mAdView = (AdView) findViewById(R.id.adView);
    // Sets Snake class to receive events from the AdView.
    mAdView.setAdListener(this);
    // Creates a Test Request
    AdRequest testAdRequest = new AdRequest();
    // Sets up a set of Test Devices, in this case, the emulator only.
    HashSet<String> testDevices = new HashSet<String>();
    testDevices.add(AdRequest.TEST_EMULATOR);
    testAdRequest.setTestDevices(testDevices);
    // Make the Ad Request.
    mAdView.loadAd(testAdRequest);

    // END AD REQUEST CODE
    
    if (savedInstanceState == null) {
      // We were just launched -- set up a new game
      mSnakeView.setMode(SnakeView.READY);
    } else {
      // We are being restored
      Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
      if (map != null) {
        mSnakeView.restoreState(map);
      } else {
        mSnakeView.setMode(SnakeView.PAUSE);
      }
    }
  }

  ////////////////////////////////////
  // AdListener implementations below.
  ////////////////////////////////////
  
  static private boolean useScaleAnimation = true;
  
  // When the ad is received, set up a special scaling animation to
  // "zoom" the ad in.
  @Override
  public void onReceiveAd(Ad ad) {
    if (useScaleAnimation) {
      ScaleAnimation zoomIn = new ScaleAnimation(.5f, 1f, .5f, 1f,
          Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
      zoomIn.setDuration(500);
      mAdView.startAnimation(zoomIn);
    } else {
      float distance = mAdView.getHeight();
      TranslateAnimation slideDown = new TranslateAnimation(0, 0, -distance, 0);
      slideDown.setDuration(500);
      mAdView.startAnimation(slideDown);
    }
  }

  // Pause the application when the ad is about to send the user
  // away from the application.
  @Override
  public void onLeaveApplication(Ad ad) {
    // Pause the game along with the activity
    mSnakeView.setMode(SnakeView.PAUSE);
  }

  // Pause the application because the ad is about to show something 
  // on top of the game.
  @Override
  public void onPresentScreen(Ad ad) {
    // Pause the game along with the activity
    mSnakeView.setMode(SnakeView.PAUSE);
  }

  @Override
  public void onDismissScreen(Ad ad) {
    // We choose to do nothing here.
  }
  
  @Override
  public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
    // In this case, we do nothing and let no ad show.
  }

  @Override
  protected void onPause() {
    super.onPause();
    // Pause the game along with the activity
    mSnakeView.setMode(SnakeView.PAUSE);
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    // Store the game state
    outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
  }

}
